package com.hbnu.controller;

import com.hbnu.pojo.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@RestController//表示控制器里面返回的所有都是数据不是视图，RestController注解相当于Controller注解和ResponseBody注解的组合
@Controller
public class HomeController {
    
    @RequestMapping("/home")
    public String homepage(){
        return "home";//返回的是页面的名字
    }

    @RequestMapping("/param1")
    @ResponseBody //表示返回的是数据不是视图
    public void param1(String name,Integer age){
        System.out.println("接收到的数据:" + name + "--->" + age);
    }

    @RequestMapping("/param2")
    @ResponseBody
    public void param2(User user){
        System.out.println("接收到的表单数据：" +user);
    }

    @RequestMapping("/param3")
    @ResponseBody
    public void param3(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("接收到的时间：" + date);
        System.out.println("格式化后的时间:" + simpleDateFormat.format(date));
    }


    //自定义时间格式
    @InitBinder //初始数据绑定
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }


    @RequestMapping("/forward")
    public String testForward(){
        System.out.println("我处理不了，我找人帮您处理......");
        return "forward:home";
    }

    @RequestMapping("/redirect")
    public String testRedirect(){
        System.out.println("我不想处理，你去找别人处理......");
        return "redirect:home";
    }


    @RequestMapping(value = "/character",produces = "text/json;charset=utf-8")
    @ResponseBody
    public String testCharacter(){
        return "我是中文数据，那你能看懂吗？";
    }


    @RequestMapping("/userInfo")
    public String userInfo(Model model){
        model.addAttribute("name","jack");
        model.addAttribute("age",17);
        return "home";
    }


    @RequestMapping("/findAllUser")
    @ResponseBody
    public List<User> findAllUser(){
        List<User> userList = new ArrayList<User>();
        userList.add(new User(1,"xiaoli","湖北黄冈",56894.00));
        userList.add(new User(2,"xiaoye","湖北黄冈",99999.00));
        userList.add(new User(3,"xiaowang","湖北黄冈",88888.00));
        userList.add(new User(4,"xiaozhang","湖北黄冈",77777.00));
        userList.add(new User(5,"xiaoxu","湖北黄冈",66666.00));

        return userList;
    }
}
